# Build recipe for lm-sensors.
#
# Copyright (c) 2018, 2019, 2021 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=lm-sensors
version=3-6-0
release=1

pkgversion="$(echo $version | tr - .)"

# Define a category for the output of the package name
pkgcategory=tools

tarname=${program}-${version}.tar.gz

# Remote source(s)
fetch=https://github.com/lm-sensors/lm-sensors/archive/V${version}/$tarname
#fetch=https://ftp.gwdg.de/pub/linux/misc/lm-sensors/$tarname

description="
Hardware monitoring support.

The lm-sensors package provides support for the hardware monitoring
drivers in the Linux Kernel.  This is useful for monitoring the
temperature of the CPU and adjusting the performance of some hardware
(such as cooling fans).
"

homepage=https://hwmon.wiki.kernel.org/lm_sensors
license="GPLv2+, LGPLv2.1"

# Source documentation
docs="CHANGES CONTRIBUTORS COPYING* README doc/fancontrol.txt"
docsdir="${docdir}/${program}-${pkgversion}"

build()
{
    unpack "${tardir}/$tarname"

    cd $srcdir

    make -j${jobs} V=1 MACHINE=$arch LIBICONV=-lc \
     BUILD_STATIC_LIB=0 \
     PREFIX=/usr LIBDIR=/usr/lib${libSuffix} MANDIR=$mandir \
     DESTDIR="$destdir" install

    # Strip remaining binaries and libraries
    find "$destdir" -type f | xargs file | \
     awk '/ELF/ && /executable/ || /shared object/' | \
      cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

    #strip --strip-debug "${destdir}/usr/lib${libSuffix}"/*.a

    # To manage (dot) new config via graft(1)
    touch "${destdir}/etc/.graft-config"

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

